/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comparator;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import javax.sql.DataSource;
import moviedownloader5.DataSourceProvider;

/**
 *
 * @author Patrik
 */
public class Matcher {
    
    private class Film{
        
        private int film_id;
        private List<String> names;
        
        public Film(int film_id, List<String> names){
            this.film_id = film_id;
            this.names = names;
        }
        
        public void setFilmId(int film_id){
            this.film_id = film_id;
        }
        
        public void setNames(List<String> names){
            this.names = names;
        }
        
        public int getFilmId(){
            return this.film_id;
        }
        
        public List<String> getNames(){
            return this.names;
        }
    }
    
    private class CompareHelper{
        
        private Film film;
        private final Show show;
        
        private double maximum = 0;
        
        public CompareHelper(Show show, Film film){
            this.show = show;
            this.film = film;
            
            this. maximum = countMaximum(show, film.getNames());
        }
        
        public void setFilm(Film film){
            this.film = film;
        }
        
        public void setMaximum(double maximum){
            this.maximum = maximum;
        }
        
        public double getMaximum(){
            return this.maximum;
        }
        
        public Film getFilm(){
            return this.film;
        }
        
        public Show getShow(){
            return this.show;
        }

        @Override
        public String toString() {
            String film_names = "";
            for(String str: film.getNames()){
                film_names += str;
                if(film.getNames().indexOf(str) != (film.getNames().size() - 1)){
                    film_names += "  -|-  ";
                }
            }
            return "Show Name: " + show.getName() + "| Film Names: " + film_names + " | " + maximum;
        }
        
    }
    
    public double countMaximum(Show show, List<String> names) {
        double maximum = 0;

        String show_reduced_name = "";
        if (show.getName().contains(":")) {
            show_reduced_name = MovieNameFilter.processMovieName(show.getName());
        }
        for (String str : names) {
            double temp_max = comparator.compareStrings(show.getName(), str.split("[(]")[0]);
            
            if (temp_max > maximum) {
                maximum = temp_max;
            }
            
            if (!show_reduced_name.equals("")) {
                temp_max = comparator.compareStrings(show_reduced_name, str.split("[(]")[0]);
                if (temp_max > maximum) {
                    maximum = temp_max;
                }
            }
        }

        return maximum;
    }
        
    private final DataSource ds;
    private final Connection conn;
    
   // private final Shows shows;
    private final Films films;
    private final Comparator comparator;
    
    public Matcher(Films films, int limit, int offset) {
      //  this.shows = new Shows(limit, offset);
        this.films = films;
        this.comparator = new Comparator();
        
        this.ds = DataSourceProvider.getDataSource();
        try {
            conn = ds.getConnection();
            conn.setAutoCommit(true);
        } catch (SQLException ex) {
            throw new IllegalArgumentException(ex);
        }
    }
   /* 
    public void match(){
        
        HashMap<Show, CompareHelper> result = new HashMap<>();
        
        CompareHelper compHelp;
        Show show;
        
        for (int i = 0; i < shows.getSize(); i++) {
            show = shows.getShow(i);
            compHelp = null;
            
            String show_reduced_name = "";
            if(show.getName().contains(":")){
                    show_reduced_name = MovieNameFilter.processMovieName(show.getName());
            }
            
            for (int key : films.getFilms().keySet()) {
                
                List<String> film_names = films.getFilms().get(key);
                
                for (String str : film_names) {
                    if (!show_reduced_name.equals("")) {
                        if (comparator.match(str.split("[(]")[0], show.getName()) || comparator.match(str.split("[(]")[0], show_reduced_name)) {
                            if (compHelp != null) {
                                double max = countMaximum(show, film_names);
                                
                                if (max > compHelp.getMaximum()) {
                                    compHelp.getFilm().setFilmId(key);
                                    compHelp.getFilm().setNames(film_names);
                                    compHelp.setMaximum(max);
                                }
                            } else {
                                compHelp = new CompareHelper(show, new Film(key, film_names));
                            }
                        }
                    }else{
                        if (comparator.match(str.split("[(]")[0], show.getName())) {
                            if (compHelp != null) {
                                double max = countMaximum(show, film_names);
                                
                                if (max > compHelp.getMaximum()) {
                                    compHelp.getFilm().setFilmId(key);
                                    compHelp.getFilm().setNames(film_names);
                                    compHelp.setMaximum(max);
                                }
                            } else {
                                compHelp = new CompareHelper(show, new Film(key, film_names));
                            }
                        }
                    }
                }
            }
            
            if (compHelp != null) {
                try {
                    Statement show_statement = conn.createStatement();
                    show_statement.executeQuery("update show set film_id = " + compHelp.getFilm().getFilmId() + " where show_id = " + compHelp.getShow().getId());

                } catch (SQLException ex) {
                }
            }
        }
        
       /* System.out.println("Result > ");
        System.out.println("================================");
        
        for (Show key : result.keySet()) {
            CompareHelper help = result.get(key);
            System.out.println(help.toString());
        }
        
        System.out.println("================================");
                */
        /*
        for (int key : films.getFilms().keySet()) {
                
                List<String> film_names = films.getFilms().get(key);
                
                for (String str : film_names) {
                    if (!show_reduced_name.equals("")) {
                        if (comparator.match(str.split("[(]")[0], show.getName()) || comparator.match(str.split("[(]")[0], show_reduced_name)) {
                            if (result.containsKey(show)) {
                                double max = countMaximum(show, film_names);
                                
                                if (max > result.get(show).getMaximum()) {
                                    Film film = result.get(show).getFilm();

                                    film.setFilmId(key);
                                    film.setNames(film_names);

                                    result.get(show).setMaximum(max);
                                }
                            } else {
                                CompareHelper helper = new CompareHelper(show, new Film(key, film_names));

                                result.put(show, helper);
                            }
                        }
                    }else{
                        if (comparator.match(str.split("[(]")[0], show.getName())) {
                            if (result.containsKey(show)) {
                                double max = countMaximum(show, film_names);
                                
                                if (max > result.get(show).getMaximum()) {
                                    Film film = result.get(show).getFilm();

                                    film.setFilmId(key);
                                    film.setNames(film_names);

                                    result.get(show).setMaximum(max);
                                }
                            } else {
                                CompareHelper helper = new CompareHelper(show, new Film(key, film_names));

                                result.put(show, helper);
                            }
                        }
                    }
                }
            }*/
   // }
    
}
